import numpy as np
import sklearn
from sklearn.manifold import TSNE
from sklearn.datasets import load_digits

# Random state.
RS = 20150101

import matplotlib.pyplot as plt
import matplotlib.patheffects as PathEffects
import matplotlib

# We import seaborn to make nice plots.
import seaborn as sns

sns.set_style('darkgrid')
sns.set_palette('muted')
sns.set_context("notebook", font_scale=1.5,
                rc={"lines.linewidth": 2.5})


def tsne_plot(xs):
    print(f'tsne_plot len:{xs.shape[0]}')
    y = np.array([d[1] for d in xs])
    xs = np.array([d[0] for d in xs])

    digits_proj = TSNE(random_state=RS).fit_transform(xs)
    scatter(digits_proj, y)
    # plt.savefig('digits_tsne-generated.png', dpi=120)
    plt.show()


def scatter(x, y):
    # We choose a color palette with seaborn.
    palette = np.array(sns.color_palette("hls", 12))

    # We create a scatter plot.
    f = plt.figure(figsize=(8, 8))
    ax = plt.subplot(aspect='equal')
    sc = ax.scatter(x[:, 0], x[:, 1], lw=0, s=40, c=palette[y.astype(np.int)])
    plt.xlim(-25, 25)
    plt.ylim(-25, 25)
    ax.axis('off')
    ax.axis('tight')
    return f, ax, sc
